package com.twu.biblioteca;

/**
 * Created with IntelliJ IDEA.
 * User: dbelsey
 * Date: 01/02/13
 * Time: 10:07
 */
public class UserData {
    private String name, password, emailAddress, phoneNumber;

    public UserData(String name, String password, String emailAddress, String phoneNumber) throws InvalidUserDataException{
        this.phoneNumber = validatePhoneNumber(phoneNumber);
        this.emailAddress = validateEmailAddress(emailAddress);
        this.name = name;
        // TODO: quick hack - storing passwords like this is the stupidest thing ever
        this.password = password;
    }

    private String validateEmailAddress(String emailAddress) throws InvalidUserDataException{
        return validate(emailAddress,  "[\\d\\w]+@[\\d\\w]+(.[a-zA-Z]{2,3})+", "Email address");
    }

    private String validatePhoneNumber(String phoneNo) throws InvalidUserDataException {
        return validate(phoneNo, "0\\d{4}(\\s)?\\d{3}(\\s?)\\d{3}", "Phone number");
    }

    private String validate(String stringToTest, String validationMask, String fieldName) throws InvalidUserDataException{
        if(stringToTest.matches(validationMask))
            return stringToTest;
        else throw new InvalidUserDataException(fieldName);
    }

    public String getName(){
        return name;
    }
    public String getEmailAddress() {
        return emailAddress;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public String getPassword() {
        return password;
    }

    public boolean checkPassword(String enteredPassword) {
        return password.equals(enteredPassword);
    }

}
